package com.drinks.yiniang.ui.widget.nav;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.common.listener.OnCategoryNavgationBarClickListener;
import com.hansen.library.utils.AppUtils;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * 分类页面navigation bar
 * Created by han on 2018/10/17 0017.
 */

public class NavigationCategoryBarLayout extends ConstraintLayout implements View.OnClickListener {

    private Context mContent;

    private AppCompatImageView mBackView;
    private AppCompatTextView mTitleTextView;
    private AppCompatImageView mSearchView;
    private AppCompatImageView mShoppingCartView;

    private int mTitleTextSize;
    private int mTitleTextColor;

    private int mBackResId;
    private int mSearchResId;
    private int mCartResId;

    private int navBarHeight;

    private String mTitleText;

    private boolean showImageBack;

    private OnCategoryNavgationBarClickListener onNavgationBarClickListener;

    public NavigationCategoryBarLayout(Context context) {
        this(context, null);
    }

    public NavigationCategoryBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationCategoryBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initData();
    }

    private void initView(Context content, AttributeSet attrs) {
        mContent = content;

        TypedArray typedArray = mContent.obtainStyledAttributes(attrs, R.styleable.NavigationBarLayoutStyleable);

        mTitleText = typedArray.getString(R.styleable.NavigationBarLayoutStyleable_navBarTitleText);


        mTitleTextSize = typedArray.getInt(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextSize, 18);
        mTitleTextColor = typedArray.getColor(R.styleable.NavigationBarLayoutStyleable_navBarTitleTextColor, ColorUtils.getColorById(mContent, R.color.color_282829));

        showImageBack = typedArray.getBoolean(R.styleable.NavigationBarLayoutStyleable_navBarShowImageBack, true);

        mBackResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarBackResId, R.mipmap.icon_back);
        mSearchResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarBackResId, R.mipmap.icon_nav_search_black);
        mCartResId = typedArray.getResourceId(R.styleable.NavigationBarLayoutStyleable_navBarBackResId, R.mipmap.icon_nav_cart_black);


        typedArray.recycle();

        initTest();
    }

    private void initData() {
        int dp5 = ScreenSizeUtils.dp2px(mContent, 5);
        int dp10 = ScreenSizeUtils.dp2px(mContent, 10);
        int dp50 = ScreenSizeUtils.dp2px(mContent,50);
        //获取顶部navbar高度
        navBarHeight = mContent.getResources().getDimensionPixelSize(R.dimen.height_navigation_bar);

        int statusBarHeight = 0;

        //沉浸，设置状态栏高度, 加上&& !isInEditMode() 判断是因为在EditMode模式下，会出现exception
        if (AppUtils.isVersionCodeGreaterThanOrEqualToKITKAT() && !isInEditMode()) {
            statusBarHeight = (int) ScreenSizeUtils.getStatusBarHeightWithAndroid(mContent);
            setPadding(0, statusBarHeight, 0, 0);
        }

        if (showImageBack) {
            mBackView = new AppCompatImageView(mContent);

            mBackView.setId(R.id.iv_nav_bar_back);
            mBackView.setPadding(dp5, 0, dp5, 0);
            mBackView.setImageResource(mBackResId > 0 ? mBackResId : R.mipmap.icon_back);
            mBackView.setOnClickListener(this);

            addView(mBackView);
        }

        mTitleTextView = new AppCompatTextView(mContent);

        mTitleTextView.setId(R.id.tv_nav_bar_title);
        mTitleTextView.setGravity(Gravity.CENTER);
        mTitleTextView.setLines(1);
        mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
        mTitleTextView.setTextColor(mTitleTextColor);
        mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTitleTextSize);
        mTitleTextView.setText(mTitleText);
        mTitleTextView.setCompoundDrawablePadding(5);
        mTitleTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_arrow_triangle_down, 0);
        mTitleTextView.setOnClickListener(this);

        addView(mTitleTextView);

        mSearchView = new AppCompatImageView(mContent);
        mShoppingCartView = new AppCompatImageView(mContent);

        mSearchView.setId(R.id.iv_nav_bar_search);
        mSearchView.setPadding(dp5, 0, dp5, 0);
        mSearchView.setImageResource(mSearchResId);
        mSearchView.setOnClickListener(this);

        addView(mSearchView);

        mShoppingCartView.setId(R.id.iv_nav_bar_cart);
        mShoppingCartView.setPadding(dp5, 0, dp5, 0);
        mShoppingCartView.setImageResource(mCartResId);
        mShoppingCartView.setOnClickListener(this);

        addView(mShoppingCartView);

        //添加依赖关系
        ConstraintSet set = new ConstraintSet();

        set.clone(this);

        //title
        set.constrainWidth(mTitleTextView.getId(), ConstraintSet.WRAP_CONTENT);
        set.constrainHeight(mTitleTextView.getId(), navBarHeight);
        set.connect(mTitleTextView.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, dp50);
        set.connect(mTitleTextView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mTitleTextView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, dp50);

        //icon back
        if(mBackView != null) {
            set.constrainWidth(mBackView.getId(), ConstraintSet.WRAP_CONTENT);
            set.constrainHeight(mBackView.getId(), navBarHeight);
            set.connect(mBackView.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, dp10);
            set.connect(mBackView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        }

        //shopping cart view
        set.constrainWidth(mShoppingCartView.getId(), ConstraintSet.WRAP_CONTENT);
        set.constrainHeight(mShoppingCartView.getId(), navBarHeight);
        set.connect(mShoppingCartView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mShoppingCartView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, dp10);

        //search view
        set.constrainWidth(mSearchView.getId(), ConstraintSet.WRAP_CONTENT);
        set.constrainHeight(mSearchView.getId(), navBarHeight);
        set.connect(mSearchView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
        set.connect(mSearchView.getId(), ConstraintSet.RIGHT, mShoppingCartView.getId(), ConstraintSet.LEFT);

        set.applyTo(this);
    }

    private void initTest() {
        if (isInEditMode()) {
            setBackgroundColor(ContextCompat.getColor(mContent, R.color.color_nav_bar));
            if (mTitleText == null) {
                mTitleText = "商品分类";
            }
            initData();
        }
    }

    @Override
    public void onClick(View v) {
        if (onNavgationBarClickListener == null) {
            return;
        }

        if (v.getId() == R.id.iv_nav_bar_back) { //返回
            onNavgationBarClickListener.onBackClick(v);
        } else if (v.getId() == R.id.tv_nav_bar_title) { //标题
            onNavgationBarClickListener.onTitleClick(v);
        }else if (v.getId() == R.id.iv_nav_bar_search) { //搜索
            onNavgationBarClickListener.onSearchClick(v);
        }else if (v.getId() == R.id.iv_nav_bar_cart) { //购物车
            onNavgationBarClickListener.onShoppingCart(v);
        }
    }

    //设置标题
    public void setNavBarTitle(int resId) {
        if (mTitleTextView != null) {
            mTitleTextView.setText(resId);
        }
    }

    //设置标题
    public void setNavBarTitle(String text) {
        if (mTitleTextView != null) {
            mTitleTextView.setText(text);
        }
    }

    public void setOnNavgationBarClickListener(OnCategoryNavgationBarClickListener onNavgationBarClickListener) {
        this.onNavgationBarClickListener = onNavgationBarClickListener;
    }
}
